package com.xinyue.ext.reader;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;

import com.xinyue.ext.data.DaoData;
import com.xinyue.ext.data.EntityData;
import com.xinyue.ext.exception.ExceptionBase;
import com.xinyue.ext.reader.bean.BeanReader;
import com.xinyue.ext.reader.dao.DaoReader;

public class CoreReader {
	//异常信息
	private static ExceptionBase exception;
	
	//私有构造器
	private CoreReader() {
	}
	//内部类
	private static class SingletonHolder{
        private static CoreReader instance = new CoreReader();  
    }
	/**
	 * 获得对象实例
	 */
	public static CoreReader getInstance(ExceptionBase exception) {
		CoreReader.exception = exception;
		return SingletonHolder.instance;
	}

	
	/**
	 * 读取一个XML文件中的Entity标签信息，并返回封装对象
	 * @param entity
	 * @param beanPackage
	 * @param daoPackage
	 * @return
	 * @throws Exception
	 */
	public List<EntityData> read(List<Element> entites, String beanPackage, String daoPackage) throws Exception {
		//读取一个XML文件中的所有Entity标签数据
		List<EntityData> datas = new ArrayList<EntityData>();
		
		for (Element entity: entites) {
			//参数集
			ParamHolder holder = new ParamHolder();
			holder.setException(exception);
			holder.setEntity(new EntityData());
			holder.getEntity().setDao(new DaoData());
			
			holder.getEntity().setBeanPackage(beanPackage);
			holder.getEntity().getDao().setDaoPackage(daoPackage);
			
			//读取Bean信息
			BeanReader beanReader = BeanReader.getInstance(holder);
			beanReader.read(entity);
			//读取Dao信息
			DaoReader daoReader = DaoReader.getInstance(holder);
			daoReader.read(entity);
			
			datas.add(holder.getEntity());
		}
		return datas;
	}
}
